<?php

namespace App\Service\NotifyLog;


use App\Library\Common;
use App\Model\AppUser;
use App\Model\FlyNotifyLog;
use App\Model\FlyShop;
use App\Service\BaseService;
use Hyperf\Di\Annotation\Inject;

class NotifyLogService extends BaseService
{

    /**
     * Notes:
     * @param int $admin_id
     * @param string $is_read
     * @param int $type  1用户 2商铺
     * @param int $up_time
     * @return \Hyperf\Database\Model\Builder
     * Date: 2022/8/24
     * Time:15:53
     * Auth: zdl
     */
    public function list(int $admin_id,$type=1,$up_time=0)
    {
        $where = [];
        $n_model =  New FlyNotifyLog();
//        获取全部通知
        if($type==1){
            if($up_time==1){
                AppUser::query()
                    ->where('id',$admin_id)
                    ->update(['last_notify_time'=>Common::date_time(2)]);
            }
            return $n_model
                ->where('is_all','1')
                ->whereIn('to_type',[1,2])
                ->orWhere('to_user',$admin_id)
                ->orderBy('id','DESC');
        }else{
            if($up_time==1){
                FlyShop::query()->where('id',$admin_id)->update(['last_notify_time'=>Common::date_time(2)]);
            }
            return $n_model
                ->where('is_all','1')
                ->whereIn('to_type',[1,2])
                ->orWhere('to_user',$admin_id)
                ->orderBy('id','DESC');
        }
    }

    public function info($id,$admin_id,$type=1){

        $n_model =  New FlyNotifyLog();
        $info = $n_model->where('id',$id)->first();
        if($type==1){
            $info->user_read_time = Common::date_time(2);
            $info->user_is_read =1;
            $info->save();
        }else{
            $info->shop_read_time = Common::date_time(2);
            $info->shop_is_read =1;
            $info->save();
        }
        return $info;
    }

    public function count_size($admin_id,$type=1,$date_time=''){
        $n_model =  New FlyNotifyLog();
        if($date_time==''){
            $date_time = '0000-00-00 00:00:00';
        }
        $where = [];
        $where['is_all']  = '1';
        $where[]  = ['created_at','>',$date_time];
        if($type==1){
            return  $n_model
                ->where($where)
                ->whereIn('to_type',[1,2])
                ->union(
                    $n_model
                    ->where('to_user',$admin_id)
                    ->where('created_at','>',$date_time)
                )
                ->count();
        }else{
            return  $n_model
                ->where($where)
                ->whereIn('to_type',[1,3])
                ->union(
                    $n_model
                        ->where('to_user',$admin_id)
                        ->where('created_at','>',$date_time)
                )->count();
        }
    }


}